<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>客户管理</a>
        <a>
          <cite>已分配客户</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="layui-row"
         style="padding-left:20px;margin: 10px 0 -10px 0;;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane" style="margin-bottom: -3px;">
            <div class="layui-input-inline">
                <input type="text" name="keywords" placeholder="请输入搜索关键词" autocomplete="off" class="layui-input">
            </div>
            {if $Type_Student != 5}
            <div class="layui-input-inline">
                <select name="user_id" lay-search="" lay-filter="user_id">
                    <option value="">请选择跟进人</option>
                    {volist name="users" id="user"}
                    <option value="{$user.id}">{$user['name']}</option>
                    {/volist}
                </select>
            </div>
            {/if}
            <div class="layui-input-inline">
                <input type="text" name="day" id="date" style="width: auto;" placeholder="下次联系时间" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select name="intentional_grade" lay-search="" lay-filter="intentional_grade">
                    <option value="">请选择成熟度</option>
                    <option value="0">0类</option>
                    <option value="1">1类</option>
                    <option value="2">2类</option>
                    <option value="3">3类</option>
                    <option value="4">4类</option>
                    <option value="5">5类</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>
            </div>
        </div>
    </div>
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['element','table',  'upload', 'laydate', 'layer','form'], function () {
        let table = layui.table,
                element = layui.element,
                laydate = layui.laydate,
                form = layui.form,
                layer = layui.layer,
                $ = layui.$;

        //限定可选日期
        laydate.render({
            elem: '#date'
            // ,max: day_date
            ,range: true
            ,trigger: 'click'
        });

        //日期
        laydate.render({
            elem: '#start',
            type: 'date'
            ,trigger: 'click'
        });

        let loading = layer.load();
       
         //数据表格 方法级渲染
            let tableIns = table.render({
                elem: '#datalist'
                , url: '{:url("Customer/assigned_index")}'
                , method: 'POST'
                , loading: true
                , parseData: function (res) { //res 即为原始返回的数据
                    if (res.code === 1) {
                        res.code = 0;
                    } else {
                        res.code = -1;
                    }
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.data //解析数据列表
                    };
                },
                cols: [[
                    {field: 'name', title: '书店名称',style:'color:#01AAED;font-weight: bold;',width:200, align: 'center', event: 'setSign'},
                    {field: 'address', title: '省市区',minWidth:150, align: 'center'},
                    {field: 'phone', title: '联系电话',width:150, align: 'center'},
                    {field: 'customer_name', title: '联系人',width:100, align: 'center'},
                    {field: 'intentional_grade_text', title: '客户成熟度',width:120, align: 'center', sort: true},
                    {field: 'next_follow_up_time', title: '下次联系时间',width:170, align: 'center', sort: true},
                    {field: 'follow_up_time', title: '最后联系时间',width:170, align: 'center', sort: true},
                    {field: 'register_date', title: '入库日期',width:120, align: 'center', sort: true},
                    {field: 'course_consultant', title: '跟进人',width:100, align: 'center'},
                ]]
                , page: true
                , limit: 10
                , done: function () {
                    layer.close(loading);
                }
            });


        //领取客户
        $('#receive').click(function () {
            let checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
            if (checkStatus.data.length == 0) {
                layer.msg('请至少选择一条数据', {icon: 7, time: 2000}, function () {
                });
                return false;
            }

            let checked_ids = [];
            for (let index = 0; index < checkStatus.data.length; index++) {
                const element = checkStatus.data[index];
                //该客户是系统或者无效则无法领取
                if (element.course_consultant_type == 1 || element.status == 2) {
                    layer.msg('所选数据中已存在归属或无效数据,请重新选择', {icon: 7, time: 2000}, function () {
                        tableIns.reload();
                    });
                    return false;
                }
                checked_ids.push(element.id)
            }


            let data = {"ids": checked_ids.join(',')};
            let url = '{:url("Customer/receive_customer")}';
            $.post(url,data,function(response){
                if(response.code === 1){
                    layer.msg(response.msg, {icon: 1, time: response.wait * 1000},function () {
                        tableIns.reload();
                    });
                }
            });
        });

        //监听单元格事件
        table.on('tool(Event)', function (obj) {
            let data = obj.data; //获得当前行数据
            let layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'setSign'){
                layer.open({
                    type: 2,
                    skin: 'layui-layer-lan',
                    closeBtn: 2,
                    title: '客户详情',
                    area: ['80%', '80%'],
                    shadeClose: false,
                    content: "{:url('Customer/information')}?id=" + data.id + "&status="+1
                });
            }
        });

        //搜索项
        $('#search').click(function () {
            let field = {};
            field.stype = $('select[name=stype] option:selected').val();
            field.intentional_grade = $('select[name=intentional_grade] option:selected').val();
            field.user_id = $('select[name=user_id] option:selected').val();
            field.day = $('input[name=day]').val();
            field.keywords = $('input[name=keywords]').val();
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //清除
        $('#clear').click(function () {
            $('.layui-input').each(function (index, element) {
                $(element).val('');
            })
            tableIns.reload({
                where: {
                    'keywords': '',
                    'intentional_grade': '',
                    'user_id': '',
                    'day': ''
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

    });
</script>